﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace GestorDeFlotasDesktop.AsignChoferAuto
{
    public partial class Listado_ACA_Chofer : Form
    {
        private DataTable dt;
        private DateTime fecha;
        private int auto_id;
        private int chofer_id;
        private int turno_id;
        private string auto_text;
        private string chofer_text;
        private string turno_text;

        public Listado_ACA_Chofer(DateTime fec, int auto, string auto_txt, int chofer, string chofer_txt, int turno, string turno_txt)
        {
            fecha = fec;
            auto_id = auto;
            chofer_id = chofer;
            turno_id = turno;
            auto_text = auto_txt;
            chofer_text = chofer_txt;
            turno_text = turno_txt;

            InitializeComponent();

            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NUNCA_TAXI.Choferes WHERE Ch_habilitado = 1;", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dt.Columns.Add("Seleccionar");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["Seleccionar"] = "Seleccionar";
                }
                
                try
                {
                    Modelo.Modelo.openConnection(con);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Modelo.Modelo.closeConnection(con);
                }
            }
            

        }

        private void Listado_Chofer_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 9)
            {
                DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[0];
                DataGridViewCell cell_nombre = dataGridView1.Rows[e.RowIndex].Cells[1];
                DataGridViewCell cell_apellido = dataGridView1.Rows[e.RowIndex].Cells[2];
                Login.Login.mainForm.openForm(new AsignChoferAuto(fecha, auto_id, auto_text, Convert.ToInt32(cell.Value.ToString()), String.Concat(cell_nombre.Value.ToString(), " - ", cell_apellido.Value.ToString()), turno_id, turno_text));
            }
        }

        private void limpiar_Click(object sender, EventArgs e)
        {
            tb_apellido.Text = tb_dni.Text = tb_nombre.Text = "";
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NUNCA_TAXI.Choferes WHERE Ch_habilitado = 1;", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dt.Columns.Add("Seleccionar");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["Seleccionar"] = "Seleccionar";
                }

                try
                {
                    Modelo.Modelo.openConnection(con);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Modelo.Modelo.closeConnection(con);
                }
            }
        }

        private void aceptar_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = Modelo.Modelo.createConnection())
            {
                string dni;
                if (tb_dni.Text == "")
                {
                    dni = "0";
                }
                else
                {
                    dni = tb_dni.Text;
                }
                SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM NUNCA_TAXI.Choferes WHERE Ch_Nombre LIKE '" + tb_nombre.Text + "' OR Ch_Apellido LIKE '" + tb_apellido.Text + "' OR Ch_Dni = " + dni + " AND Ch_habilitado = 1;", con);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dt.Columns.Add("Seleccionar");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["Seleccionar"] = "Seleccionar";
                }
                dataGridView1.DataSource = dt;
            }
        }
    }
}
